<template>
  <div class="app-container">
    <div class="filter-container">  <!-- 增加 删除 按钮  -->
      <el-form>
        <el-form-item>
          <el-button type="success" icon="plus" v-if="hasPerm('devices:add')" @click="showCreate" style='margin-left:100px; '>增加设备
          </el-button>
          
          <!-- 问题:根据输入的内容 搜索 匹配 显示相关的内容  还有就是搜索框根据浏览器的视图大小而变化 还需要稍微修改  dev-->  
          
          <el-input v-model="search" placeholder="请输入搜索设备内容" style='width: 50%; margin-left:200px'></el-input>
          <el-button type="primary" icon="el-icon-search" circle style='margin-left:10px;' @click="lists()"></el-button>
        </el-form-item>
      </el-form>
    </div>

<!-- 这是一个 表格 -->   
    <el-table
      :data="lists" v-loading.body="listLoading" element-loading-text="拼命加载中" border fit highlight-current-row
      style="width: 100%;">    
    <el-table-column align="center" prop="devId" label="设备编号">
      </el-table-column>
      <el-table-column align="center" prop="devModel" label="设备型号">
      </el-table-column>
      <el-table-column align="center" prop="devName" label="设备名称">
      </el-table-column>
      <el-table-column align="center" prop="devBrand" label="设备品牌">
      </el-table-column>
      <el-table-column align="center" prop="devType" label="类型/属性">
      </el-table-column>
      <el-table-column align="center" prop="devPosition" label="使用位置">
      </el-table-column>
      <el-table-column align="center" prop="devSituation" label="设备情况">
      </el-table-column>
      <el-table-column align="center" prop="createTime" label="创建时间">
      </el-table-column>
      <el-table-column align="center" prop="userName" label="维修人员">
      </el-table-column>
      <el-table-column align="center" prop="devRemark" label="备注">
      </el-table-column>
      <el-table-column align="center" label="操作">
      <template slot-scope="scope">
        <!--<el-button type="danger" size="small" v-if="hasPerm('devices:delete')" @click="removeDev(scope.$index)">删除设备</el-button>-->
        <el-button type="danger" size="small" @click="removeDev(scope.$index)">删除设备</el-button>
      </template>
    </el-table-column>
    </el-table>

    <!-- 分页器 -->
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="listQuery.pageNum"
      :page-size="listQuery.pageRow"
      :total="totalCount"
      :page-sizes="[10, 20, 50, 100]"
      layout=" sizes, prev, pager, next, jumper">
    </el-pagination>

    <!-- 这是一个button 点击弹框 -->
    <el-dialog :title="textMap[dialogStatus]" :visible.sync="dialogFormVisible">
      <el-form class="small-space" :model="tempDevice" label-position="left" label-width="100px"
               style='width: 600px; margin-left:50px;'>
        <el-form-item label="设备编号" required>
          <el-input type="text" v-model="tempDevice.devId" style="width: 250px;">
          </el-input>
        </el-form-item>
        <el-form-item label="设备型号" required>
          <el-input type="text" v-model="tempDevice.devModel" style="width: 250px;">
          </el-input>
        </el-form-item>
        <el-form-item label="设备名称" required>
          <el-input type="text" v-model="tempDevice.devName" style="width: 250px;">
          </el-input>
        </el-form-item>
        <el-form-item label="设备品牌" required>
          <el-input type="text" v-model="tempDevice.devBrand" style="width: 250px;">
          </el-input>
        </el-form-item>
        <el-form-item label="类型/属性" required>
          <el-input type="text" v-model="tempDevice.devType" style="width: 250px;">
          </el-input>
        </el-form-item>
        <el-form-item label="使用位置" required>
          <el-input type="text" v-model="tempDevice.devPosition" style="width: 250px;">
          </el-input>
        </el-form-item>
        <el-form-item label="设备情况" required>
            <el-select v-model="tempDevice.devSituation" placeholder="请选择" style="width:250px;">
            <el-option
              v-for="st in SituationType"
              :key="st.value"
              :label="st.label"
              :value="st.value">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="维修人员ID" required>
          <el-select v-model="tempDevice.devAdminId" clearable placeholder="请选择" style="width:250px;">
            <el-option
              v-for="item in listDev"
              :key="item.devAdminId"
              :label="item.label"
              :value="item.devAdminId">
            </el-option>
          </el-select>
          <p style="color:#848484;">说明:红色权限为对应菜单内的必选权限</p>
        </el-form-item>  
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="success" @click="createDevice">创 建</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import {mapGetters} from 'vuex'

  export default {
    data() {
      return {
        search: '',
        listDev: '',
        listDev:{
          value: '',
          label: '',
        },
        totalCount: 0,
        list: [],//表格的数据
        listLoading: false,//数据加载等待动画
        dialogStatus: 'create',
        dialogFormVisible: false,
        listQuery: {
          pageNum: 1, //页码
          pageRow: 50, //每页条数
        },
        SituationType:[{
          value: '空闲中',
          label: '空闲中'
        },{
          value: '使用中',
          label: '使用中'
        },{
          value: '维护中',
          label: '维护中'
        },{
          value: '停用中',
          label: '停用中'
        }],
        textMap: {
          create: '新建设备'
        },
        tempDevice: {
          devId: '',
          devModel: '',
          devName: '',
          devBrand: '',
          devType: '',
          devPosition: '',
          devSituation: '',
          devAdminId: '',
          permissions: [],
        },
      }
    },
    created() {
      this.getList();
      //this.getListDevUser();
    },
    computed: {
      // 模糊搜索
      lists () {
        const search = this.search
        if (search) {
          // filter() 方法创建一个新的数组，新数组中的元素是通过检查指定数组中符合条件的所有元素。
          // 注意： filter() 不会对空数组进行检测。
          // 注意： filter() 不会改变原始数组。
          return this.list.filter(data => {
            // some() 方法用于检测数组中的元素是否满足指定条件;
            // some() 方法会依次执行数组的每个元素：
            // 如果有一个元素满足条件，则表达式返回true , 剩余的元素不会再执行检测;
            // 如果没有满足条件的元素，则返回false。
            // 注意： some() 不会对空数组进行检测。
            // 注意： some() 不会改变原始数组。
            return Object.keys(data).some(key => {
              // indexOf() 返回某个指定的字符在某个字符串中首次出现的位置，如果没有找到就返回-1；
              // 该方法对大小写敏感！所以之前需要toLowerCase()方法将所有查询到内容变为小写。
              return String(data[key]).toLowerCase().indexOf(search) > -1
            })
          })
        }
        return this.list
      }
    },
    methods: {
      getList() {
        //查询列表
        this.listLoading = true;
        this.api({
          url: "/devices/listDevices",
          method: "get",
          params: this.listQuery
        }).then(data => {
          this.listLoading = false;
          this.list = data.list;
          console.log(data);
          this.totalCount = data.totalCount;
        })
      },
      getListDevUser() {
        //查询列表
        this.listLoading = true;
        this.api({
          url: "/user/listDeviceUser",
          method: "get",
          params: this.listQuery
        }).then(data => {
          this.listLoading = false;
          this.listDev = data.list;
          console.log(this.listDev);
          this.totalCount = data.totalCount;
        })
      },
      handleSizeChange(val) {
        //改变每页数量
        this.listQuery.pageRow = val
        this.handleFilter();
      },
      handleCurrentChange(val) {
        //改变页码
        this.listQuery.pageNum = val
        this.getList();
      },
      handleFilter() {
        //查询事件
        this.listQuery.pageNum = 1
        this.getList()
      },
      getIndex($index) {
        //表格序号
        return (this.listQuery.pageNum - 1) * this.listQuery.pageRow + $index + 1
      },
      showCreate() {
        //显示新增对话框
        this.tempDevice.devModel = '';
        this.tempDevice.devId = '';
        this.tempDevice.devName = '';
        this.tempDevice.devBrand = '';
        this.tempDevice.devPosition = '';
        this.tempDevice.devType = '';
        this.tempDevice.devSituation = '',
        this.tempDevice.devAdminId = '',
        this.dialogStatus = "create"
        this.dialogFormVisible = true
      },
      showUpdate($index) {    
        let device = this.list[$index];
        this.tempDevice.devModel = device.devModel;
        this.tempDevice.devId = device.devId;
        this.tempDevice.devType = device.devType;
        this.tempDevice.devBrand = device.devBrand;
        this.tempDevice.devName = device.devName;
        this.tempDevice.devPosition = device.devPosition;
        this.tempDevice.devSituation = device.devSituation;
        this.dialogStatus = "update"
        this.dialogFormVisible = true
      },
      removeDev($index) {
        let _vue = this;
        this.$confirm('确定删除此设备?', '提示', {
          confirmButtonText: '确定',
          showCancelButton: false,
          type: 'warning'
        }).then(() => {
          let device = _vue.list[$index];
          device.deleteStatus = '2';
          _vue.api({
            url: "/devices/deleteDevices",
            method: "post",
            data: device,
          }).then(() => {
            _vue.getList()
          }).catch(e => {
            _vue.$message.error("删除失败")
          })
        })
      },
      createDevice() {
        if (!this.checkDeviceNameUnique()) {
          return;
        }
        //添加新角色
        this.api({
          url: "/devices/addDevices",
          method: "post",
          data: this.tempDevice
        }).then(() => {
          this.getList();
          this.dialogFormVisible = false
        })
      },
      updateDevice() {
        if (!this.checkDeviceNameUnique(this.tempDevice.devId)) {
          return;
        }
        //修改角色
        this.api({
          url: "/devices/updateDevices",
          method: "post",
          data: this.tempDevice
        }).then(() => {
          this.getList();
          this.dialogFormVisible = false
        })
      },
      checkDeviceNameUnique(devId) {
        //校验名称重复
        let devIds = this.tempDevice.devId;
        if (!devIds) {
          this.$message.error("请填写设备编号");
          return false;
        }
        let devices = this.list;
        let result = true;
        for (let j = 0; j < devices.length; j++) {
          if (devices[j].devIds === devIds) {
            this.$message.error("设备编号已存在");
            result = false;
            break;
          }
        }
        return result;
      },
    }
  }
</script>
<style scoped>
  .requiredPerm {
    color: #ff0e13;
  }
</style>
